package com.sotb.sender;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
/**
 * 
 * @author brave
 *
 */
public class RecordActivity extends Activity{
	private ImageButton playBtn;
	private ImageButton deleteBtn;
	private ListView myListView1;
	private File myRecAudioDir;
	private File myPlayFile;
	private static String SubPatb = "SOTB";
	private static String SDCardPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
	
	private ArrayList<String> recordFiles;
	private ArrayAdapter<String> adapter;
	private TextView myTextView1;
	private boolean sdCardExit;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.recording);
	
	  playBtn = (ImageButton) findViewById(R.id.PlayBtn);
	  deleteBtn = (ImageButton) findViewById(R.id.DeleteBtn);
	  myListView1 = (ListView) findViewById(R.id.ListView01);
	  myTextView1 = (TextView) findViewById(R.id.TextView01);
	  playBtn.setEnabled(false);
	  deleteBtn.setEnabled(false);
	
	  /* 判断SD Card是否插入 */
	  sdCardExit = Environment.getExternalStorageState().equals(
	      android.os.Environment.MEDIA_MOUNTED);
	  /* 取得SD Card路径做为录音的文件位置 */
	  if (sdCardExit)
	    myRecAudioDir = new File(SDCardPath + SubPatb);
	
	  /* 取得SD Card目录里的所有.amr文件 */
	  getRecordFiles();
	
	  adapter = new ArrayAdapter<String>(this,
	      R.layout.record_list_item, recordFiles);
	  /* 将ArrayAdapter存入ListView对象中 */
	  myListView1.setAdapter(adapter);
	
	  
	  /* 播放 */
	  playBtn.setOnClickListener(new ImageButton.OnClickListener()
	  {
	
	    //@Override
	    public void onClick(View arg0)
	    {
	      // TODO Auto-generated method stub
	      if (myPlayFile != null && myPlayFile.exists())
	      {
	        /* 开启播放的程序 */
	        openFile(myPlayFile);
	      }
	
	    }
	  });
	  /* 删除 */
	  deleteBtn.setOnClickListener(new ImageButton.OnClickListener(){
		  //  @Override
		  public void onClick(View arg0){
			  // TODO Auto-generated method stub
		      if (myPlayFile != null)
		      {
		        /* 因将Adapter移除文件名 */
		        adapter.remove(myPlayFile.getName());
		        /* 删除文件 */
		        if (myPlayFile.exists())
		        myPlayFile.delete();
		        myTextView1.setText("完成删除");
		      }
		  }
	  });
	
	  myListView1.setOnItemClickListener(new AdapterView.OnItemClickListener(){
		  // @Override
	      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){	    	  
	    	  /* 当有点选文件名时将删除及播放按钮Enable */
	    	  playBtn.setEnabled(true);
	          deleteBtn.setEnabled(true);	
	          myPlayFile = new File(myRecAudioDir.getAbsolutePath() + File.separator + ((CheckedTextView) arg1).getText());
	          myTextView1.setText("你选的是：" + ((CheckedTextView) arg1).getText());
	      }
	  });
	}
		
	private void getRecordFiles()
	{
	  recordFiles = new ArrayList<String>();
	  if (sdCardExit)
	  {
	    File files[] = myRecAudioDir.listFiles();
	    if (files != null)
	    {
	
	      for (int i = 0; i < files.length; i++)
	      {
	        if (files[i].getName().indexOf(".") >= 0)
	        {
	          /* 读取.amr文件 */
	          String fileS = files[i].getName().substring(files[i].getName().indexOf("."));
	          if (fileS.toLowerCase().equals(".amr"))
	            recordFiles.add(files[i].getName());	
	        }
	      }
	    }
	  }
	}
	
	/* 开启播放录音文件的程序 */
	private void openFile(File f)
	{
	  Intent intent = new Intent();
	  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	  intent.setAction(android.content.Intent.ACTION_VIEW);
	
	  String type = getMIMEType(f);
	  intent.setDataAndType(Uri.fromFile(f), type);
	  startActivity(intent);
	}
	
	private String getMIMEType(File f)
	{
	  String end = f.getName().substring(f.getName().lastIndexOf(".") + 1, f.getName().length()).toLowerCase();
	  String type = "";
	  if (end.equals("mp3") || end.equals("aac") || end.equals("aac") || end.equals("amr") || end.equals("mpeg") || end.equals("mp4"))
	  {
		  type = "audio";
	  } else if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg")){
	    type = "image";
	  } else
	  {
	    type = "*";
	  }
	  type += "/*";
	  return type;
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		finish();
	}
}
